<?php
namespace app\admin\controller\final_accounts;

use app\admin\traits\Curd;
use app\admin\model\FinalAccountsType;
use app\admin\model\FinalAccountsClassify;
use app\admin\model\FinalAccountsIndex;
use app\common\controller\AdminController;
use think\App;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\annotation\ControllerAnnotation;
use app\admin\service\DateService;

/**
 * Class Echart
 * @package app\admin\controller\final_accounts
 * @ControllerAnnotation(title="决算类型")
 */

class Echart extends AdminController
{
    use Curd;

    public $type_arr = [
        '当期现金净流入'=> 1,
        '当期支出'=> 2,
        '当期毛利'=> 3,
        '当期毛利率'=> 4,
        '当期负债'=> 5,
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
    }


    /**
     * @NodeAnotation(title="列表")
     */
    
    public function index(){


        $class_arr = FinalAccountsClassify::field('id,title')->where('pid',0)->select()->toArray();
        $str_time = DateService::getlastMonthDays(date('Y-m-d'),false);
        $this->assign('class_name', $class_arr);
        $this->assign('year', date('Y'));
        $this->assign('class_time', $str_time[1].' - '.$str_time[1]);
        $this->assign('start_time', $str_time[0].' - '.$str_time[0]);
        $this->assign('end_time', $str_time[1].' - '.$str_time[1]);
        return $this->fetch();
    }

    public function getClassYear(){
        $time = $this->request->get('time',date('Y'));
        $class_id = $this->request->get('type',0);
        $status = $this->request->get('status',0);
        $status_class = $this->request->get('status_class',1);
        $class_list = FinalAccountsClassify::getClass();
        if(empty($class_list)) return json(['code' => 200, 'msg' => 'success', 'data' => []]);
        $year_data = DateService::actionYear($time);
        
        if($class_id){
            if($status){
                $class_list[$class_id] = [$class_id];
            }
        
            foreach($year_data as $value){
                $actual = FinalAccountsIndex::whereIn('classify_id', $class_list[$class_id])
                    ->where('time', '>=', strtotime($value['first_day']))
                    ->where('status', $status_class)
                    ->where('time', '<', strtotime($value['last_day'].' 23:59:59'))
                    ->sum('total');
                $keys = explode('-',$value['first_day'])[1].'月';
                $data['name'][] = $keys;
                $data['val'][] = $actual;
            }
        }else{
            foreach($year_data as $value){
                $actual = FinalAccountsIndex::where('time', '>=', strtotime($value['first_day']))
                    ->where('time', '<', strtotime($value['last_day'].' 23:59:59'))
                    ->where('status', $status_class)
                    ->sum('total');
                $keys = explode('-',$value['first_day'])[1].'月';
                $data['name'][] = $keys;
                $data['val'][] = $actual;
            }
        }
        return json(['code'=> 200,'msg' => 'success','data' => $data]);
    }


    public function getClassData(){
        $time = $this->request->get('time');
        $class_id = $this->request->get('type',0);
        $status = $this->request->get('status',0);
        if($time == '') $time = date('Y-m',time()).' - '.date('Y-m',time());
        $str_time = explode(' - ',$time);
        $last_time = DateService::getDate($str_time[1])['last_day'].' 23:59:59';
        $class_list = FinalAccountsClassify::getClass();
        if(empty($class_list)) return json(['code' => 200, 'msg' => 'success', 'data' => []]);
        if($class_id){
            if($status){
                $class_list_arr = [$class_id];
            }else{
                $class_list_arr = $class_list[$class_id];
            }
            foreach ($class_list_arr as $value){
                $budge = FinalAccountsIndex::where('classify_id', $value)
                        ->where('status', 1)
                        ->where('time', '>=', strtotime($str_time[0]))
                        ->where('time', '<', strtotime($last_time))
                        ->sum('total');
                $actual = FinalAccountsIndex::where('classify_id', $value)
                        ->where('status', 2)
                        ->where('time', '>=', strtotime($str_time[0]))
                        ->where('time', '<', strtotime($last_time))
                        ->sum('total');
                $title[] = FinalAccountsClassify::where('id',$value)->value('title');
                $array[] = [$budge,$actual];
            }
        }else{
            foreach ($class_list as $key => $value){
                $budge = FinalAccountsIndex::whereIn('classify_id', $value)
                        ->where('status', 1)
                        ->where('time', '>=', strtotime($str_time[0]))
                        ->where('time', '<', strtotime($last_time))
                        ->sum('total');
                $actual = FinalAccountsIndex::whereIn('classify_id', $value)
                        ->where('status', 2)
                        ->where('time', '>=', strtotime($str_time[0]))
                        ->where('time', '<', strtotime($last_time))
                        ->sum('total');
                $title[] = FinalAccountsClassify::where('id',$key)->value('title');
                $array[] = [$budge,$actual];
            }
        }
            
        $keys = ['决算费用','预算费用'];
        $series = DateService::getDataChange($array,$keys);
        $data = [
            'name' => $title ?? [],
            'keys' => $keys,
            'series' => $series,
        ];

        return json(['code'=> 200,'msg' => 'success','data' => $data]);

    }
    
    public function getContrastData(){
        $time = $this->request->get('time');
        $class_id = $this->request->get('type',0);
        $status = $this->request->get('status',0);
        $status_class = $this->request->get('status_class',1);
        
        $str_time = explode(' - ',$time);
        //2021-08 - 2021-09,2021-08 - 2021-09
        if(count(array_unique($str_time)) === 1) return json(['code'=> 500,'msg' => '环比不能选择相同月份','data' => '']);
        $keys = [
            $str_time[0].'至'. $str_time[1],
            $str_time[2].'至'. $str_time[3],
        ];
        if(count(array_unique($keys)) === 1) return json(['code'=> 500,'msg' => '环比不能选择相同月份','data' => '']);
        $class_list = FinalAccountsClassify::getClass();
        if(empty($class_list)) return json(['code' => 200, 'msg' => 'success', 'data' => []]);
        $last_time_arr = [
            1 => DateService::getDate($str_time[1])['last_day'].' 23:59:59',
            3 => DateService::getDate($str_time[3])['last_day'].' 23:59:59'
        ];

        if($class_id){
            if($status){
                $class_list_arr = [$class_id];
            }else{
                $class_list_arr = $class_list[$class_id];
            }
            foreach ($class_list_arr as $value){
                $budge = FinalAccountsIndex::where('classify_id', $value)
                        ->where('status', $status_class)
                        ->where('time', '>=', strtotime($str_time[0]))
                        ->where('time', '<', strtotime($last_time_arr[1]))
                        ->sum('total');
                $actual = FinalAccountsIndex::where('classify_id', $value)
                        ->where('status', $status_class)
                        ->where('time', '>=', strtotime($str_time[2]))
                        ->where('time', '<', strtotime($last_time_arr[3]))
                        ->sum('total');
                $title[] = FinalAccountsClassify::where('id',$value)->value('title');
                $array[] = [$budge,$actual];
            }
        }else{
            foreach ($class_list as $key => $value){
                $f = FinalAccountsIndex::whereIn('classify_id', $value)
                        ->where('status', $status_class)
                        ->where('time', '>=', strtotime($str_time[0]))
                        ->where('time', '<', strtotime($last_time_arr[1]))
                        ->sum('total');
                $e = FinalAccountsIndex::whereIn('classify_id', $value)
                        ->where('status', $status_class)
                        ->where('time', '>=', strtotime($str_time[2]))
                        ->where('time', '<', strtotime($last_time_arr[3]))
                        ->sum('total');
                $title[] = FinalAccountsClassify::where('id',$key)->value('title');
                $array[] = [$f,$e];
            }
        }
        $series = DateService::getDataChange($array,$keys);
        foreach($series as $key => $value){
            $series_arr[] = [
                'name' => $key,
                'data' => $value
            ];
        }
        $data = [
            'time' => $keys,
            'name' => $title ?? [],
            'series_arr' => $series_arr,
        ];

        return json(['code'=> 200,'msg' => 'success','data' => $data]);


    }

    public function getClassEr(){
        $class_id = $this->request->get('type',0);
        if($class_id){
            $data = FinalAccountsClassify::field('id,title')->where('pid',$class_id)->select()->toArray();
        }else{
            $data = [];
        }
        return json(['code'=> 200,'msg' => 'success','data' => $data]);
    }


    


}





































?>